package com.platform.qujia.service.impl;

import com.platform.qujia.constantEnum.Constant;
import com.platform.qujia.controller.CouponController;
import com.platform.qujia.dao.AppUserSuperMapper;
import com.platform.qujia.pojo.AppUserInfo;
import com.platform.qujia.pojo.AppUserSuper;
import com.platform.qujia.pojo.AppUserSuperDetail;
import com.platform.qujia.pojo.SuperPayOrder;
import com.platform.qujia.service.AppUserInfoService;
import com.platform.qujia.service.AppUserSuperDetailService;
import com.platform.qujia.service.AppUserSuperService;

import com.platform.qujia.service.CouponService;
import com.platform.qujia.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;


/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author qujia
 * @date 2021-11-30
 */
@Service("appUserSuperService")
public class AppUserSuperServiceImpl implements AppUserSuperService
{
    @Resource
    private AppUserSuperMapper appUserSuperMapper;

    @Resource
    private AppUserInfoService appUserInfoService;

    @Resource
    private AppUserSuperDetailService appUserSuperDetailService;
    @Resource
    private CouponService couponService;
    @Resource
    private CouponController couponController;

    @Override
    public String insertAppUserSuperPhone(AppUserSuper appUserSuper, SuperPayOrder superPayOrder) {
        String[] phons=appUserSuper.getPhone().split("\n");
        StringBuilder builder=new StringBuilder();
        String mobilePhone =  superPayOrder.getMobilePhone();
        String couponIssueIds =   appUserSuper.getCouponIssueIds();
        List<String> couponIds = Collections.emptyList();
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(couponIssueIds)) {
            String[] split = couponIssueIds.split(",");
            couponIds = Arrays.asList(split);
        }
        try {
            couponController.drawCouponsForSuper(mobilePhone, couponIds, Constant.COUPON_USER_BIZ_TYPE_SUPER_USER,superPayOrder.getSuperPayId(),7);
        } catch (Exception e) {
            // 在这里处理异常，可以打印错误信息或执行其他操作
            System.out.println(e);
        }

        for (String item:phons ) {
            AppUserInfo info=appUserInfoService.getAppUserInfo(AppUserInfo.builder().stationId(Integer.valueOf(appUserSuper.getEnterpriseId().toString()))
                    .mobilePhone(appUserSuper.getPhone()).build());
            if(info ==null || StringUtils.isEmpty(info.getUserId().toString())){
                builder.append("暂无"+item+"手机号会员\n");
                continue;
            }
            //判断如果之前是超级会员，endDate在之前的基础上增加
            List<AppUserSuperDetail> appUserSuperDetails = appUserSuperDetailService.selectAppUserSuperDetailList(AppUserSuperDetail.builder().enterpriseId(appUserSuper.getEnterpriseId())
                    .userId(Long.valueOf(info.getUserId().toString())).build());
//            AppUserSuperDetail appUserSuperDetail = appUserSuperDetailService.selectAppUserSuperDetailById(Long.valueOf(info.getUserId().toString()));
            AppUserSuperDetail appUserSuperDetail = null;
            if(appUserSuperDetails != null && appUserSuperDetails.size()>0){
                appUserSuperDetail = appUserSuperDetails.get(0);
            }
            if(appUserSuperDetail != null){
                Date endDate = appUserSuperDetail.getEndDate();
                appUserSuperDetail.setOpeningMode(1);
                appUserSuperDetail.setEnable(0);
                appUserSuperDetail.setPayOrder(superPayOrder.getSuperOrderNo());
                appUserSuperDetail.setCreatby(info.getName());
                if(endDate.before(new Date())){
                    //已经过期
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(new Date());
                    cal.add(Calendar.DATE,  appUserSuper.getCycle().intValue());
                    appUserSuperDetail.setEndDate(cal.getTime());
                    appUserSuperDetail.setSuperId(appUserSuper.getSuperId());
                    appUserSuperDetailService.updateAppUserSuperDetail(appUserSuperDetail);
                }else{
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(endDate);
                    cal.add(Calendar.DATE,  appUserSuper.getCycle().intValue());
                    appUserSuperDetail.setEndDate(cal.getTime());
                    appUserSuperDetail.setSuperId(appUserSuper.getSuperId());
                    appUserSuperDetailService.updateAppUserSuperDetail(appUserSuperDetail);
                }
            }else{
                AppUserSuperDetail detail=new AppUserSuperDetail();
                detail.setBeginDate(new Timestamp(System.currentTimeMillis()));
                Date date = new Date();
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.DATE,  appUserSuper.getCycle().intValue());
                detail.setEndDate(cal.getTime());
                detail.setUserId(Long.valueOf(info.getUserId().toString()));
                detail.setSuperId(appUserSuper.getSuperId());
                detail.setCreatby(info.getName());
                detail.setEnable(0);
                detail.setOpeningMode(1);
                detail.setEnterpriseId(appUserSuper.getEnterpriseId());
                detail.setPayOrder(superPayOrder.getSuperOrderNo());
                detail.setCreatedate(new Date());
                appUserSuperDetailService.insertAppUserSuperDetail(detail);
            }
            couponService.userSuperCoupon(info,appUserSuper);
        }
        return builder.toString();
    }


    @Override
    public AppUserSuper selectAppUserSuperById(Long superId) {
        return appUserSuperMapper.selectAppUserSuperById(superId);
    }

    @Override
    public List<AppUserSuper> getAppUserSuperList(AppUserSuper appUserSuper) {
        return appUserSuperMapper.selectAppUserSuperList(appUserSuper);
    }
}
